<template>
    <el-dialog class="model-build-dialog" width="80%" :visible="visible"
               append-to-body @close="handleClose" @closed="$emit('input', false)">

        <el-row slot="title" type="flex" justify="space-between">
            <el-col :span="6">
                <span>模型构建</span>
            </el-col>
            <el-col class="center" :span="6">
                <el-button type="text" size="mini" @click="goModelToJs">前往 模型转JS</el-button>
            </el-col>
        </el-row>

        <el-form ref="form" :model="data" size="mini" label-width="150px">
            <el-form-item label="类名：" prop="name">
                <el-input class="input" v-model.trim="data.className" @change="rebuild"></el-input>
                <el-radio-group v-model="classType" size="mini" @change="rebuild">
                    <el-radio-button label="">空</el-radio-button>
                    <el-radio-button label="Bean"></el-radio-button>
                    <el-radio-button label="Entity"></el-radio-button>
                    <el-radio-button label="VO"></el-radio-button>
                    <el-radio-button label="DTO"></el-radio-button>
                    <el-radio-button label="Model"></el-radio-button>
                    <el-radio-button label="Domain"></el-radio-button>
                    <el-radio-button label="DO"></el-radio-button>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="表注释：" prop="name">
                <el-input class="input" v-model.trim="data.comment"></el-input>
            </el-form-item>

            <el-form-item label="其他：" prop="name">
                <el-checkbox v-model="isValid" @change="build">验证信息</el-checkbox>
                <el-checkbox v-model="isExtra" @change="build">额外信息</el-checkbox>
            </el-form-item>
        </el-form>

        <monaco-editor v-model="result" lang="java" height="800px"></monaco-editor>

        <div slot="footer">
            <el-button :loading="saveLoading" @click="handleClose">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script type="text/ecmascript-6">
  import MonacoEditor from "../../../components/editor/MonacoEditor/index";
  import { MODEL } from "../../../res/config-key";
  import { defaultTemplate } from "../default-template";
  import { local } from "../../../utils/js/storage";
  import { firstToUpper, toHump } from "../../../utils/js/text";
  import * as Mustache from "mustache";
  import { handleDataType, handleExtra, handleValid } from "./handle";

  /**
   * @author 白雨浓
   * @date 2019/7/8 14:07
   *
   * 模型构建 窗口
   **/
  export default {
    name: 'ModelBuildDialog',
    components: {MonacoEditor},
    props: ['value', 'table', 'columns'],
    data() {
      return {
        visible: this.value,
        saveLoading: false,
        template: defaultTemplate,
        isValid: false,
        isExtra: true,
        classType: '',
        data: {},
        result: '',
      }
    },
    created() {
      const template = local.getItem(MODEL.TEMPLATE);

      if (template) {
        this.template = template;
      }
    },
    mounted() {
      this.build();
    },
    methods: {
      build() {

        this.data = {
          className: firstToUpper(toHump(this.table.TABLE_NAME)),
          classType: this.classType,
          comment: this.table.TABLE_COMMENT,
          variables: this.columns.map(column => {

            const javaDataType = handleDataType(column.DATA_TYPE, column.COLUMN_TYPE);

            return {
              name: toHump(column.COLUMN_NAME.toLowerCase()),
              dataType: javaDataType,
              comment: column.COLUMN_COMMENT,
              extra: this.isExtra ? handleExtra(column) : null,
              valid: this.isValid ? handleValid(javaDataType, column.COLUMN_COMMENT) : null,
            }
          }),
        };

        let result = Mustache.render(this.template, this.data);
        result = result.replace(/&lt;/g, "<");
        result = result.replace(/&gt;/g, ">");

        this.result = result;
      },
      rebuild() {
        this.data.classType = this.classType;
        this.result = Mustache.render(this.template, this.data);
      },
      goModelToJs() {
        this.$router.push({name: 'ModelJs', params: {model: this.result}})
      },
      handleClose() {
        this.visible = false;
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .model-build-dialog {
    }

    .center {
        text-align center
    }

    .input {
        width 80%
    }
</style>
